<?php

namespace App\Http\Api\Controllers;

use App\Exceptions\InvalidRequestException;
use App\Http\Admin\Services\Order\OrderService;
use App\Http\Controllers\Controller;
use App\Lib\DingDingNotice\Liver;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;


class LiverController extends Controller
{
    public $service;
    public function __construct(OrderService $service)
    {
        $this->service = $service;
    }

    public function liver(Request $request){

        $type = $request->input('type') ?? 1;
        $sign = $request->input('sign') ?? '';
        $attributes = [];
        if($type == 1){ //默认昨日6点到今日6点
            if(date("Y-m-d H:i:s") >  date("Y-m-d").' 06:00:00'){
                $attributes['start_time']   =   date("Y-m-d").' 06:00:00';
                $attributes['end_time']     =   date("Y-m-d", strtotime("+1 day")).' 06:00:00';
            }else{
                $attributes['start_time']   =   date("Y-m-d", strtotime("-1 day")).' 06:00:00';
                $attributes['end_time']     =   date("Y-m-d").' 06:00:00';
            }

        }elseif ($type == 2){
            if(date("Y-m-d H:i:s") >  date("Y-m-d").' 06:00:00'){
                $attributes['start_time']   =   date("Y-m-d", strtotime("-1 day")).' 06:00:00';
                $attributes['end_time']     =   date("Y-m-d").' 06:00:00';
            }else{
                $attributes['start_time']   =   date("Y-m-d", strtotime("-2 day")).' 06:00:00';
                $attributes['end_time']     =   date("Y-m-d", strtotime("-1 day")).' 06:00:00';
            }

        }

        $type   =   intval($type);
        $day    =   $type;
        if(!empty($sign)){
            $day    =   $day+1;
            $attributes['start_time']   =   date("Y-m-d", strtotime("-$day day")).' 06:00:00';
            $attributes['end_time']     =   date("Y-m-d", strtotime("-$type day")).' 06:00:00';
        }
        
        $result = $this->service->OrderLiverCount($attributes);
        //钉钉通知
        $token = '7b2c8efa77286b729bde4cc7817c42343ed80f66f1df3cde0bf3e0d1a67ce7f5';
        $liver = new Liver($day,$attributes['start_time'],$attributes['end_time'],$result,$token);
        $result = $liver->sendMarkdownMessage();
        return $result;
    }


}
